package com.slz3.business.module.memberManagement.memRecord.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.memberManagement.memRecord.domain.MemRecord;
import com.slz3.business.module.memberManagement.memRecord.domain.MemRecordFactory;
import com.slz3.business.module.memberManagement.memRecord.steward.MemRecordSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class MemRecordServiceImpl implements MemRecordService {

    @Override
    public MemRecord save(MemRecord memRecord) {
        return Repositorys.memRecord.save(MemRecordFactory.initBaseFields(memRecord));
    }

    @Override
    public Page<MemRecord> page(MultiValueMap<String, String> params) {
        Page<MemRecord> memRecordPage = Repositorys.memRecord.findAll(
                MemRecordSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return memRecordPage;
    }

    @Override
    public MemRecord modify(MemRecord memRecordNew) {
        return Repositorys.memRecord.saveAndFlush(MemRecordFactory.initBaseFields(memRecordNew));
    }

    @Override
    public Optional<MemRecord> single(UUID uuid) {
        return Repositorys.memRecord.findById(uuid);
    }

    @Override
    public List<MemRecord> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.memRecord.findAllById(uuids);
    }

    @Override
    public List<MemRecord> delete(List<MemRecord> memRecordList) {
        memRecordList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            MemRecordFactory.initBaseFields(item);
        });
        return Services.memRecordTransactional.batchModify(memRecordList);
    }

}
